package com.tcoiss.datafactory.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import com.tcoiss.datafactory.domain.BillType;
import com.tcoiss.datafactory.service.IBillTypeService;
import com.tcoiss.common.core.web.page.TableDataInfo;

/**
 * 单据类型配置Controller
 * 
 * @author zw
 * @date 2021-05-27
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/billType" )
public class BillTypeController extends BaseController {

    private final IBillTypeService iBillTypeService;

    /**
     * 查询单据类型配置列表
     */
    @PreAuthorize(hasPermi = "datafactory:billType:list")
    @GetMapping("/list")
    public TableDataInfo list(BillType billType) {
        startPage();
        List<BillType> list = iBillTypeService.queryList(billType);
        return getDataTable(list);
    }

    /**
     * 查询单据类型配置
     */
    @GetMapping("/getBillTypes")
    public List<BillType> getTables(BillType billType) {
        return iBillTypeService.queryList(billType);
    }
    /**
     * 导出单据类型配置列表
     */
    @PreAuthorize(hasPermi = "datafactory:billType:export" )
    @Log(title = "单据类型配置" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,BillType billType) throws IOException
    {
        List<BillType> list = iBillTypeService.queryList(billType);
        ExcelUtil<BillType> util = new ExcelUtil<BillType>(BillType.class);
        util.exportExcel(response,list, "billType" );
    }

    /**
     * 获取单据类型配置详细信息
     */
    @PreAuthorize(hasPermi = "datafactory:billType:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBillTypeService.getById(id));
    }

    /**
     * 新增单据类型配置
     */
    @PreAuthorize(hasPermi = "datafactory:billType:add" )
    @Log(title = "单据类型配置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BillType billType) {
        return toAjax(iBillTypeService.save(billType) ? 1 : 0);
    }

    @PreAuthorize(hasPermi = "datafactory:billType:syncBill" )
    @Log(title = "单据类型配置" , businessType = BusinessType.INSERT)
    @PostMapping("/syncBill")
    public AjaxResult syncBill() {
        return toAjax(iBillTypeService.syncBillType() ? 1 : 0);
    }

    /**
     * 修改单据类型配置
     */
    @PreAuthorize(hasPermi = "datafactory:billType:edit" )
    @Log(title = "单据类型配置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BillType billType) {
        return toAjax(iBillTypeService.updateById(billType) ? 1 : 0);
    }

    /**
     * 删除单据类型配置
     */
    @PreAuthorize(hasPermi = "datafactory:billType:remove" )
    @Log(title = "单据类型配置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBillTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
